import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:sample2/helper/chat_helper.dart';
import 'package:sample2/pages/chat/anonymous/anonymous_chat_list.dart';
import 'package:sample2/pages/chat/channel/chat_channel_page.dart';
import 'package:sample2/pages/chat/chat_list.dart';
import 'package:sample2/pages/chat/chat_profile.dart';

class Chat extends StatefulWidget {
  const Chat({super.key});
  @override
  State<StatefulWidget> createState() {
    return ChatState();
  }
}

class ChatState extends State<Chat> with TickerProviderStateMixin {
  late TabController _tabController;
  @override
  void initState() {
    super.initState();
    _tabController = TabController(length: 4, vsync: this);
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    var parameters = ModalRoute.of(context)!.settings.arguments;
    if (parameters != null) {
      var arguments = parameters as ChatRouterParameter;
      _tabController.index = arguments.index;
    }
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      // appBar: AppBar(
      //   title: const Text("Chat page"),
      // ),
      body: TabBarView(
        physics:
            const NeverScrollableScrollPhysics(), //remove this line to scroll
        controller: _tabController,
        children: const [
          ChatList(),
          ChatChannelPage(),
          AnonymousChatList(),
          ChatProfile(),
        ],
      ),
      bottomNavigationBar: BottomNavigationBar(
        currentIndex: _tabController.index,
        selectedItemColor: Colors.red,
        unselectedItemColor: Colors.grey.shade600,
        selectedLabelStyle: const TextStyle(fontWeight: FontWeight.w600),
        unselectedLabelStyle: const TextStyle(fontWeight: FontWeight.w600),
        type: BottomNavigationBarType.fixed,
        onTap: (value) {
          _tabController.animateTo(value);
          setState(() {});
        },
        items: const [
          BottomNavigationBarItem(icon: Icon(Icons.message), label: "Chats"),
          BottomNavigationBarItem(
              icon: Icon(Icons.group_work), label: "Channels"),
          BottomNavigationBarItem(icon: Icon(Icons.person), label: "Anonymous"),
          BottomNavigationBarItem(
              icon: Icon(Icons.account_box), label: "Profile"),
        ],
      ),
    );
  }
}

class ChatRouterParameter {
  int index = 0;
  ChatRouterParameter({required this.index});
}
